package com.fwzhang.util;

import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Objects;

public class PdfTest {

    public static void main(String[] args) {
        try {
            File fileDirectory = new File("C:/Users/Magfin/Desktop/工作/需求整理/流水Pdf转excel/流水pdf文件");
            String encryptedBasePath = "C:/Users/Magfin/Desktop/工作/需求整理/流水Pdf转excel/加密/";
            String scanPDFBasePath = "C:/Users/Magfin/Desktop/工作/需求整理/流水Pdf转excel/扫描/";
            String availableBasePath = "C:/Users/Magfin/Desktop/工作/需求整理/流水Pdf转excel/可用/";
            if (fileDirectory.isDirectory()) {
                File[] files = fileDirectory.listFiles();
                assert files != null;
                Arrays.stream(files).forEach(file -> {
                    try {
                        String fileName = file.getName();
                        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
                        if (!Objects.equals(fileType.toLowerCase(), "pdf")) {
                            System.out.println(fileName.concat(" 不是pdf"));
                            return;
                        }
                        FileInputStream fileInputStream = new FileInputStream(file);
                        PdfReader pdfReader = new PdfReader(fileInputStream);
                        if (pdfReader.isMetadataEncrypted()) {
                            System.out.println(fileName.concat(" 已加密"));
                            pdfReader.close();
                            copyFile(file, encryptedBasePath, fileName);
                            return;
                        }
                        int numberOfPages = pdfReader.getNumberOfPages();
                        boolean isScanPDF = false;
                        if (numberOfPages >= 1) {
                            byte[] pageContent = pdfReader.getPageContent(1);
                            isScanPDF = pdfReader.getPageResources(1).contains(PdfName.FONT);
                        }
                        if (isScanPDF) {
                            System.out.println(fileName.concat(" 不是扫描件"));
                            copyFile(file, availableBasePath, fileName);
                        } else {
                            System.out.println(fileName.concat(" 是扫描件"));
                            copyFile(file, scanPDFBasePath, fileName);
                        }
                        pdfReader.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                });
            }

//            PdfReader pdfReader = new PdfReader("C:/Users/Magfin/Desktop/saomiao2019.09.05.pdf");
//            PdfReader pdfReader = new PdfReader("C:/Users/Magfin/Desktop/saomiao2.pdf");
//            PdfReader pdfReader = new PdfReader("C:/Users/Magfin/Desktop/saomiao3.pdf");
//            PdfReader pdfReader = new PdfReader("C:/Users/Magfin/Desktop/戴彪的交易明细20220221153621.pdf");  CamScanner
//            PdfReader pdfReader = new PdfReader("C:/Users/Magfin/Desktop/工作/需求整理/流水Pdf转excel/可用文字pdf/赵明天流水.pdf");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void copyFile(File sourceFile, String fileBasePath, String fileName) {
        File file = new File(fileBasePath.concat(fileName));
        try (FileOutputStream fileOutputStream = new FileOutputStream(file); FileInputStream fileInputStream = new FileInputStream(sourceFile)){
            byte[] buffer = new byte[1024 * 2];
            int len = 0;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
